<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/11 0011
 * Time: 19:58
 */

namespace App\Models\Admin;


use Illuminate\Support\Facades\DB;

class DepositIn
{
    #存款金额入账表,将使用连表vshops_order和pos_order

    /**
     * 获取列表
     *
     * @param $node_id
     * @param $sid
     * @return mixed
     */
    public function getLists($node_id,$sid)
    {
        $first=DB::table('vshops_order t0')
            ->where('sid',$sid)
            ->where('t0.pay_state',2)
            ->leftJoin('tnet_reginfo t1','t1.nodeid','=','t0.nodeid')
            ->select(
                't0.order_no as order_sn',
                't0.sid as shop_node_id',
                't0.add_time',
                't0.deposit',
                't1.nodecode as mobile');
        $union=DB::table('pos_order t2')
            ->unionAll($first)
            ->where('t2.status',2)
            ->where('t2.shop_node_id', $node_id)
            ->select('t2.order_no as order_sn',
                't2.shop_node_id',
                't2.create_time as add_time',
                't2.deposit',
                't2.mobile')
            ->orderBy('add_time', 'desc')
            ->get();
        return $union;//TODO 分页
    }

    /**
     * 获取存款总金额
     *
     * @param $node_id
     * @param $sid
     * @return int
     */
    public function getTotal($node_id,$sid)
    {
        //手机订单计算
        $sum1=DB::table('vshops_order t0')
            ->where('sid',$sid)
            ->where('t0.pay_state',2)
            ->select('t0.deposit')
            ->sum('deposit');
        //POS订单计算
        $sum2=DB::table('pos_order t2')
            ->where('t2.status',2)
            ->where('t2.shop_node_id', $node_id)
            ->select('t2.deposit')
            ->sum('deposit');
        //总和
        return $sum1+$sum2;
    }

}